import pygame
import sys

# 定义常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
TOTAL_WIDTH = SCREEN_WIDTH  # 移除右侧面板，使用标准宽度

# 定义颜色
BG_COLOR = (20, 20, 20)
TEXT_COLOR = (255, 255, 255)
MENU_BG_COLOR = (40, 40, 40)
MENU_HIGHLIGHT_COLOR = (255, 255, 255)  # 选中时白色
MENU_NORMAL_COLOR = (128, 128, 128)     # 未选中时暗色
BUTTON_BG_COLOR = (60, 60, 60)
BUTTON_HOVER_COLOR = (80, 80, 80)
BUTTON_BORDER_COLOR = (100, 100, 100)

# 全局变量
menu_buttons = []  # 存储菜单按钮

def draw_menu(screen, total_width, screen_height):
    """绘制主菜单界面 - 供主游戏调用"""
    # 在函数内部加载字体，避免在模块导入时就初始化
    title_font = pygame.font.SysFont("SimSun, Microsoft YaHei", 48)
    button_font = pygame.font.SysFont("SimSun, Microsoft YaHei", 24)
    
    # 创建按钮列表
    menu_buttons = []
    
    # 绘制背景
    screen.fill(BG_COLOR)
    
    # 绘制装饰性网格
    for x in range(0, total_width, 50):
        pygame.draw.line(screen, (30, 30, 30), (x, 0), (x, screen_height), 1)
    for y in range(0, screen_height, 50):
        pygame.draw.line(screen, (30, 30, 30), (0, y), (total_width, y), 1)
    
    # 绘制标题
    title_text = title_font.render("推箱子游戏", True, TEXT_COLOR)
    title_rect = title_text.get_rect(center=(total_width // 2, screen_height // 2 - 100))
    screen.blit(title_text, title_rect)
    
    # 创建按钮
    button_width = 200
    button_height = 50
    button_x = total_width // 2 - button_width // 2
    
    # 开始游戏按钮
    start_button = pygame.Rect(button_x, screen_height // 2 - 20, button_width, button_height)
    menu_buttons.append(("start", start_button))
    
    # 游戏规则按钮
    rules_button = pygame.Rect(button_x, screen_height // 2 + 50, button_width, button_height)
    menu_buttons.append(("rules", rules_button))
    
    # 退出游戏按钮
    exit_button = pygame.Rect(button_x, screen_height // 2 + 120, button_width, button_height)
    menu_buttons.append(("exit", exit_button))
    
    # 绘制按钮
    for action, button_rect in menu_buttons:
        # 根据悬停状态选择颜色
        is_hover = button_rect.collidepoint(pygame.mouse.get_pos())
        bg_color = BUTTON_HOVER_COLOR if is_hover else BUTTON_BG_COLOR
        border_color = MENU_HIGHLIGHT_COLOR if is_hover else BUTTON_BORDER_COLOR
        text_color = MENU_HIGHLIGHT_COLOR if is_hover else MENU_NORMAL_COLOR
        
        # 绘制按钮背景
        pygame.draw.rect(screen, bg_color, button_rect)
        pygame.draw.rect(screen, border_color, button_rect, 3)
        
        # 绘制按钮文字
        if action == "start":
            text = "开始游戏"
        elif action == "rules":
            text = "游戏规则"
        elif action == "exit":
            text = "退出游戏"
        
        text_surface = button_font.render(text, True, text_color)
        text_rect = text_surface.get_rect(center=button_rect.center)
        screen.blit(text_surface, text_rect)
    
    pygame.display.flip()
    return menu_buttons

def handle_menu_click(pos, buttons):
    """处理菜单点击事件 - 供主游戏调用"""
    for action, button_rect in buttons:
        if button_rect.collidepoint(pos):
            return action
    return None

# 以下是独立运行时的代码，主游戏不会使用
if __name__ == "__main__":
    # 初始化pygame
    pygame.init()
    
    # 创建窗口
    screen = pygame.display.set_mode((TOTAL_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("推箱子游戏 - 主菜单")
    
    # 显示初始化菜单
    menu_buttons = draw_menu(screen, TOTAL_WIDTH, SCREEN_HEIGHT)
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN:
                result = handle_menu_click(event.pos, menu_buttons)
                if result:
                    print(f"选择的操作: {result}")
                    running = False
            elif event.type == pygame.MOUSEMOTION:
                # 重新绘制菜单以更新悬停效果
                menu_buttons = draw_menu(screen, TOTAL_WIDTH, SCREEN_HEIGHT)
    
    pygame.quit()
    sys.exit() 